package com.yanqu.road.entity.activity.fellingtrees.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.fellingtrees.FellingTreesEquip;
import com.yanqu.road.entity.activity.fellingtrees.FellingTreesEquipParam;
import com.yanqu.road.entity.enums.eFellingTreesEquipType;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FellingTreesConfig {

    private ActivityInfo activityInfo;

    private Map<Integer, FellingTreesElixirConfig> elixirConfigMap;             // 仙丹（buff药）
    private Map<Integer, FellingTreesLevelConfig> levelConfigMap;               // 等级（树、装备）
    private Map<Integer, FellingTreesMaterialsConfig> materialsConfigMap;       // 材料
    private Map<Integer, FellingTreesWeaponsConfig> weaponsConfigMap;           // 装备

    // 道具ID
    private int scoreItemId;                                                    // 积分
    private int goldItemId;                                                     // 金币（月露）

    // 体力
    private int maxEnergy;
    private int energyRecCount;
    private long energyRecTime;
    private int energyItemId;                                                   // 体力恢复药ID
    private List<FellingTreesMultiConfig> multiConfigList;                      // 树等级搞了，多倍消耗多点体力
    private long energyToScore;                                                 // 体力转换积分参数

    // 属性
    private BigInteger initAttack;                                              // 基础攻击力
    private int attackTimes;                                                    // 每次砍树攻击次数

    // 装备
    private int[][] posConfig;                                                  // 装备栏初始状态：-1未解锁，0解锁（注：配置表是0未解锁，1解锁）
    private int equipDropK;                                                     // 装备掉落参数，看公式，下面的也是
    private int equipDropY;
    private List<Integer> equipDropRandomWeight;
    private List<Integer> equipDropRandomLevel;
    private int maxDropEquipLevel;
    private int minDropEquipLevel;
    private int dropEquipGuaranteeQua;                                          // 法宝掉落保底品质
    private String resetPosCost;                                                // 重置装备格子消耗
    private int specialEquipUnlockLv;                                           // 灵宝解锁等级
    private String specialEquipUnlockReward;                                    // 灵宝解锁奖励
    private String daoqiUnlockReward;                                           // 道器解锁奖励

    // 格子解锁
    private List<BigInteger> unlockPosCostList;                                 // 格子解锁消耗：第x个消耗金币数量
    private Map<Integer, Integer> posSpecialEquipMap;                           // Map<格子,可佩戴灵宝数量>
    private Map<Integer, Integer> posDaoQiMap;                                  // Map<格子,可佩戴道器数量>
    private int bagSize;                                                        // 背包容量

    // 玉兔
    private int unlockRabbitNeedLevel;                                          // 解锁玉兔系统所需等级
    private String rabbitGuideReward;                                           // 玉兔引导奖励

    // 装备强化
    private int equipMaxUpgradeLv;                                              // 法宝最大强化等级
    private List<Long> equipUpgradeCost;                                        // 转给强化消耗金币参数
    private int specialEquipMaxLevel;                                           // 灵宝最大等级（这里比配置多1，配置是指强化10次）
    private List<Long> specialEquipUpgradeCost;                                 // 灵宝强化所需材料数量
    private int specialEquipUpgradeAddition;                                    // 灵宝强化每级加成

    // 新手引导
    private List<Integer> guideEquipIds;
    private List<Integer> guideEquipLvs;
    private BigInteger guideGoldCount;

    private String maxDaoqiNum;                                                 // 道器解锁格子可佩戴道器上限
    private String maxDaoqiClearParam;                                          // 道器洗练效果划分区间：千分比;权重;表现类型|千分比;权重;表现类型
    private int daoqiClearItemId;                                               // 道器洗练石月灵石道具ID

    public void initActivityConfig(Map<String, ActivityConfig> configMap) {
        // 道具ID
        setScoreItemId(configMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue());
        setGoldItemId(configMap.get("FELLING_TREES_GOLD_ITEM_ID").getIntValue());

        // 体力
        setMaxEnergy(configMap.get("FELLING_TREES_ENERGY_MAX_NUM").getIntValue());
        setEnergyRecCount(configMap.get("FELLING_TREES_ENERGY_RECOVERY_NUM").getIntValue());
        setEnergyRecTime(configMap.get("FELLING_TREES_ENERGY_RECOVERY_TIME").getIntValue() * DateHelper.SECOND_MILLIONS);
        setEnergyItemId(configMap.get("FELLING_TREES_ENERGY_ITEM_ID").getIntValue());
        List<FellingTreesMultiConfig> multiConfigList = new ArrayList<>();
        for (String multiConfigStr : configMap.get("FELLING_TREES_ENERGY_REWARD_PER").getStringListValue("\\|")) {
            List<Integer> list = StringUtils.stringToIntegerList(multiConfigStr, ";");
            multiConfigList.add(new FellingTreesMultiConfig(list.get(0), list.get(1), list.get(2) / 1000));
        }
        setMultiConfigList(multiConfigList);
        setEnergyToScore(configMap.get("FELLING_TREES_ENERGY_SCORE").getLongValue());

        // 属性
        setInitAttack(new BigInteger(configMap.get("FELLING_TREES_INIT_ATTACK").getValue()));
        setAttackTimes(configMap.get("FELLING_TREES_FELLING_WIELD_TIME").getIntValue());

        // 装备
        List<String> posStrList = StringUtils.stringToStringList(configMap.get("FELLING_TREES_DEFAULT_PANEL").getValue(), "\\|");
        int height = posStrList.size();
        int width = StringUtils.stringToIntegerList(posStrList.get(0), ",").size();
        int[][] posConfig = new int[height][width];
        for (int y = posStrList.size() - 1; y >= 0; y--) {
            List<Integer> posList = StringUtils.stringToIntegerList(posStrList.get(y), ",");
            for (int x = 0; x < posList.size(); x++) {
                posConfig[y][x] = posList.get(x) - 1; // 转化下参数
            }
        }
        setPosConfig(posConfig);
        setEquipDropK(configMap.get("FELLING_TREES_WEAPON_DROP_PERIOD").getIntValue());
        setEquipDropY(configMap.get("FELLING_TREES_WEAPON_LEVEL_PERIOD").getIntValue());
        List<Integer> randomLevelList = new ArrayList<>();
        List<Integer> randomWeightList = new ArrayList<>();
        for (String floatStr : StringUtils.stringToStringList(configMap.get("FELLING_TREES_WEAPON_FLOAT_WEIGHT").getValue(), "\\|")) {
            List<Integer> floatList = StringUtils.stringToIntegerList(floatStr, ";");
            randomLevelList.add(floatList.get(0));
            randomWeightList.add(floatList.get(1));
        }
        setEquipDropRandomLevel(randomLevelList);
        setEquipDropRandomWeight(randomWeightList);
        List<Integer> dropLevelList = configMap.get("FELLING_TREES_WEAPON_MIN_MAX_LEVEL").getIntListValue(";");
        setMinDropEquipLevel(dropLevelList.get(0));
        setMaxDropEquipLevel(dropLevelList.get(1));
        setDropEquipGuaranteeQua(configMap.get("FELLING_TREES_WEAPON_FIRST_BASE_QUALITY").getIntValue());

        // 格子解锁
        List<BigInteger> tmpCostList = new ArrayList<>();
        for (String gold : configMap.get("FELLING_TREES_UNLOCK_EQUIP_GRID").getStringListValue(";")) {
            tmpCostList.add(new BigInteger(gold));
        }
        setUnlockPosCostList(tmpCostList);

        Map<Integer, Integer> posSpecialEquipMap = new ConcurrentHashMap<>();
        for (String posEquipNum : StringUtils.stringToStringList(configMap.get("FELLING_TREES_UNLOCK_SPIRIT_BEAD_NUM").getValue(), "\\|")) {
            List<Integer> posEquipNumList = StringUtils.stringToIntegerList(posEquipNum, ";");
            posSpecialEquipMap.put(posEquipNumList.get(0), posEquipNumList.get(1));
        }
        setPosSpecialEquipMap(posSpecialEquipMap);

        Map<Integer, Integer> posDaoQiMap = new ConcurrentHashMap<>();
        for (String posDaoQiNum : StringUtils.stringToStringList(configMap.get("FELLING_TREES_MAX_DAOQI_NUM").getValue(), "\\|")) {
            List<Integer> posEquipNumList = StringUtils.stringToIntegerList(posDaoQiNum, ";");
            posDaoQiMap.put(posEquipNumList.get(0), posEquipNumList.get(1));
        }
        setPosDaoQiMap(posDaoQiMap);

        // 玉兔
        setUnlockRabbitNeedLevel(configMap.get("FELLING_TREES_UNLOCK_RABBIT").getIntValue());

        // 装备强化
        setEquipMaxUpgradeLv(configMap.get("FELLING_TREES_WEAPON_STRENGTHEN_MAX_LEVEL").getIntValue());
        setEquipUpgradeCost(configMap.get("FELLING_TREES_WEAPON_STRENGTHEN_CONSUME_LIST").getLongListValue(";"));
        setSpecialEquipMaxLevel(1 + configMap.get("FELLING_TREES_SPIRIT_BEAD_COMOPSE_MAX_LEVEL").getIntValue());
        setSpecialEquipUpgradeCost(configMap.get("FELLING_TREES_SPIRIT_BEAD_COMOPSE_CONSUME_LIST").getLongListValue(";"));
        setSpecialEquipUpgradeAddition(configMap.get("FELLING_TREES_SPIRIT_BEAD_COMOPSE_EFFECT").getIntValue());

        // 灵宝
        setSpecialEquipUnlockLv(configMap.get("FELLING_TREES_UNLOCK_SPIRIT_BEAD").getIntValue());
        setSpecialEquipUnlockReward(configMap.get("FELLING_TREES_DEFAULT_SPIRIT_BEAD").getValue());

        // 其他
        setRabbitGuideReward(configMap.get("FELLING_TREES_MAKE_BUFF_GUIDE_REWARD").getValue());
        setResetPosCost(configMap.get("FELLING_TREES_RESET_EQUIP_GRID_COST").getValue());
        setBagSize(configMap.get("FELLING_TREES_BAG_MAX_NUM").getIntValue());
        List<String> guideRewardList = configMap.get("FELLING_TREES_GUIDE_REWARD").getStringListValue("\\|");
        Property property = PropertyHelper.parseStringToProperty(guideRewardList.get(0));
        List<Integer> equipIds = new ArrayList<>();
        List<Integer> equipLvs = new ArrayList<>();
        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            equipIds.add(entry.getKey());
            equipLvs.add(entry.getValue().intValue());
        }
        setGuideEquipIds(equipIds);
        setGuideEquipLvs(equipLvs);
        setGuideGoldCount(new BigInteger(guideRewardList.get(1)));
        setMaxDaoqiNum(configMap.get("FELLING_TREES_MAX_DAOQI_NUM").getValue());
        setMaxDaoqiClearParam(configMap.get("FELLING_TREES_MAX_DAOQI_CLEAR_PARAM").getValue());
        setDaoqiClearItemId(configMap.get("FELLING_TREES_MAX_DAOQI_CLEAR_ITEM_ID").getIntValue());
        setDaoqiUnlockReward(configMap.get("FELLING_TREES_MAX_DAOQI_GUIDE_REWARD").getValue());
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public Map<Integer, FellingTreesElixirConfig> getElixirConfigMap() {
        return elixirConfigMap;
    }

    public void setElixirConfigMap(Map<Integer, FellingTreesElixirConfig> elixirConfigMap) {
        this.elixirConfigMap = elixirConfigMap;
    }

    public Map<Integer, FellingTreesLevelConfig> getLevelConfigMap() {
        return levelConfigMap;
    }

    public void setLevelConfigMap(Map<Integer, FellingTreesLevelConfig> levelConfigMap) {
        this.levelConfigMap = levelConfigMap;
    }

    public Map<Integer, FellingTreesMaterialsConfig> getMaterialsConfigMap() {
        return materialsConfigMap;
    }

    public void setMaterialsConfigMap(Map<Integer, FellingTreesMaterialsConfig> materialsConfigMap) {
        this.materialsConfigMap = materialsConfigMap;
    }

    public Map<Integer, FellingTreesWeaponsConfig> getWeaponsConfigMap() {
        return weaponsConfigMap;
    }

    public void setWeaponsConfigMap(Map<Integer, FellingTreesWeaponsConfig> weaponsConfigMap) {
        this.weaponsConfigMap = weaponsConfigMap;
    }

    public int getScoreItemId() {
        return scoreItemId;
    }

    public void setScoreItemId(int scoreItemId) {
        this.scoreItemId = scoreItemId;
    }

    public int getGoldItemId() {
        return goldItemId;
    }

    public void setGoldItemId(int goldItemId) {
        this.goldItemId = goldItemId;
    }

    public int getMaxEnergy() {
        return maxEnergy;
    }

    public void setMaxEnergy(int maxEnergy) {
        this.maxEnergy = maxEnergy;
    }

    public int getEnergyRecCount() {
        return energyRecCount;
    }

    public void setEnergyRecCount(int energyRecCount) {
        this.energyRecCount = energyRecCount;
    }

    public long getEnergyRecTime() {
        return energyRecTime;
    }

    public void setEnergyRecTime(long energyRecTime) {
        this.energyRecTime = energyRecTime;
    }

    public int getEnergyItemId() {
        return energyItemId;
    }

    public void setEnergyItemId(int energyItemId) {
        this.energyItemId = energyItemId;
    }

    public BigInteger getInitAttack() {
        return initAttack;
    }

    public void setInitAttack(BigInteger initAttack) {
        this.initAttack = initAttack;
    }

    public int[][] getPosConfig() {
        return posConfig;
    }

    public void setPosConfig(int[][] posConfig) {
        this.posConfig = posConfig;
    }

    public int getEquipDropK() {
        return equipDropK;
    }

    public void setEquipDropK(int equipDropK) {
        this.equipDropK = equipDropK;
    }

    public List<Integer> getEquipDropRandomWeight() {
        return equipDropRandomWeight;
    }

    public void setEquipDropRandomWeight(List<Integer> equipDropRandomWeight) {
        this.equipDropRandomWeight = equipDropRandomWeight;
    }

    public List<Integer> getEquipDropRandomLevel() {
        return equipDropRandomLevel;
    }

    public void setEquipDropRandomLevel(List<Integer> equipDropRandomLevel) {
        this.equipDropRandomLevel = equipDropRandomLevel;
    }

    public int getMaxDropEquipLevel() {
        return maxDropEquipLevel;
    }

    public void setMaxDropEquipLevel(int maxDropEquipLevel) {
        this.maxDropEquipLevel = maxDropEquipLevel;
    }

    public int getMinDropEquipLevel() {
        return minDropEquipLevel;
    }

    public void setMinDropEquipLevel(int minDropEquipLevel) {
        this.minDropEquipLevel = minDropEquipLevel;
    }

    public int getDropEquipGuaranteeQua() {
        return dropEquipGuaranteeQua;
    }

    public void setDropEquipGuaranteeQua(int dropEquipGuaranteeQua) {
        this.dropEquipGuaranteeQua = dropEquipGuaranteeQua;
    }

    public List<BigInteger> getUnlockPosCostList() {
        return unlockPosCostList;
    }

    public void setUnlockPosCostList(List<BigInteger> unlockPosCostList) {
        this.unlockPosCostList = unlockPosCostList;
    }

    public Map<Integer, Integer> getPosSpecialEquipMap() {
        return posSpecialEquipMap;
    }

    public void setPosSpecialEquipMap(Map<Integer, Integer> posSpecialEquipMap) {
        this.posSpecialEquipMap = posSpecialEquipMap;
    }

    public Map<Integer, Integer> getPosDaoQiMap() {
        return posDaoQiMap;
    }

    public void setPosDaoQiMap(Map<Integer, Integer> posDaoQiMap) {
        this.posDaoQiMap = posDaoQiMap;
    }

    public int getEquipMaxUpgradeLv() {
        return equipMaxUpgradeLv;
    }

    public void setEquipMaxUpgradeLv(int equipMaxUpgradeLv) {
        this.equipMaxUpgradeLv = equipMaxUpgradeLv;
    }

    public List<Long> getEquipUpgradeCost() {
        return equipUpgradeCost;
    }

    public void setEquipUpgradeCost(List<Long> equipUpgradeCost) {
        this.equipUpgradeCost = equipUpgradeCost;
    }

    public int getSpecialEquipMaxLevel() {
        return specialEquipMaxLevel;
    }

    public void setSpecialEquipMaxLevel(int specialEquipMaxLevel) {
        this.specialEquipMaxLevel = specialEquipMaxLevel;
    }

    public List<Long> getSpecialEquipUpgradeCost() {
        return specialEquipUpgradeCost;
    }

    public void setSpecialEquipUpgradeCost(List<Long> specialEquipUpgradeCost) {
        this.specialEquipUpgradeCost = specialEquipUpgradeCost;
    }

    public String getRabbitGuideReward() {
        return rabbitGuideReward;
    }

    public void setRabbitGuideReward(String rabbitGuideReward) {
        this.rabbitGuideReward = rabbitGuideReward;
    }

    public int getAttackTimes() {
        return attackTimes;
    }

    public void setAttackTimes(int attackTimes) {
        this.attackTimes = attackTimes;
    }

    public int getEquipDropY() {
        return equipDropY;
    }

    public void setEquipDropY(int equipDropY) {
        this.equipDropY = equipDropY;
    }

    public int getUnlockRabbitNeedLevel() {
        return unlockRabbitNeedLevel;
    }

    public void setUnlockRabbitNeedLevel(int unlockRabbitNeedLevel) {
        this.unlockRabbitNeedLevel = unlockRabbitNeedLevel;
    }

    public List<FellingTreesMultiConfig> getMultiConfigList() {
        return multiConfigList;
    }

    public void setMultiConfigList(List<FellingTreesMultiConfig> multiConfigList) {
        this.multiConfigList = multiConfigList;
    }

    public long getEnergyToScore() {
        return energyToScore;
    }

    public void setEnergyToScore(long energyToScore) {
        this.energyToScore = energyToScore;
    }

    public String getResetPosCost() {
        return resetPosCost;
    }

    public void setResetPosCost(String resetPosCost) {
        this.resetPosCost = resetPosCost;
    }

    public int getBagSize() {
        return bagSize;
    }

    public void setBagSize(int bagSize) {
        this.bagSize = bagSize;
    }

    public int getSpecialEquipUpgradeAddition() {
        return specialEquipUpgradeAddition;
    }

    public void setSpecialEquipUpgradeAddition(int specialEquipUpgradeAddition) {
        this.specialEquipUpgradeAddition = specialEquipUpgradeAddition;
    }

    public int getSpecialEquipUnlockLv() {
        return specialEquipUnlockLv;
    }

    public void setSpecialEquipUnlockLv(int specialEquipUnlockLv) {
        this.specialEquipUnlockLv = specialEquipUnlockLv;
    }

    public String getSpecialEquipUnlockReward() {
        return specialEquipUnlockReward;
    }

    public void setSpecialEquipUnlockReward(String specialEquipUnlockReward) {
        this.specialEquipUnlockReward = specialEquipUnlockReward;
    }

    public String getDaoqiUnlockReward() {
        return daoqiUnlockReward;
    }

    public void setDaoqiUnlockReward(String daoqiUnlockReward) {
        this.daoqiUnlockReward = daoqiUnlockReward;
    }

    public BigInteger getGuideGoldCount() {
        return guideGoldCount;
    }

    public void setGuideGoldCount(BigInteger guideGoldCount) {
        this.guideGoldCount = guideGoldCount;
    }

    public List<Integer> getGuideEquipIds() {
        return guideEquipIds;
    }

    public void setGuideEquipIds(List<Integer> guideEquipIds) {
        this.guideEquipIds = guideEquipIds;
    }

    public List<Integer> getGuideEquipLvs() {
        return guideEquipLvs;
    }

    public void setGuideEquipLvs(List<Integer> guideEquipLvs) {
        this.guideEquipLvs = guideEquipLvs;
    }

    public String getMaxDaoqiNum() {
        return maxDaoqiNum;
    }

    public void setMaxDaoqiNum(String maxDaoqiNum) {
        this.maxDaoqiNum = maxDaoqiNum;
    }

    public String getMaxDaoqiClearParam() {
        return maxDaoqiClearParam;
    }

    public void setMaxDaoqiClearParam(String maxDaoqiClearParam) {
        this.maxDaoqiClearParam = maxDaoqiClearParam;
    }

    public int getDaoqiClearItemId() {
        return daoqiClearItemId;
    }

    public void setDaoqiClearItemId(int daoqiClearItemId) {
        this.daoqiClearItemId = daoqiClearItemId;
    }

    public boolean isDaoqi(int equipId) {
        FellingTreesWeaponsConfig weaponsConfig = getWeaponsConfigMap().get(equipId);
        if (null == weaponsConfig) {
            return false;
        }
        return weaponsConfig.getType() == eFellingTreesEquipType.DaoQi.getValue();
    }

    //获得道器的星级
    public int getDaoqiStar(FellingTreesEquip equip) {
        FellingTreesWeaponsConfig weaponsConfig = getWeaponsConfigMap().get(equip.getEquipId());
        if (null == weaponsConfig) {
            return 0;
        }
        if (weaponsConfig.getType() != eFellingTreesEquipType.DaoQi.getValue()) {
            return 0;
        }
        String[] paramSplit = weaponsConfig.getParam().split("\\|");
        // 最大千分比

        int starAmount = 2;//道器本体两颗星
        List<FellingTreesEquipParam> paramList = equip.getParamList();
        if (paramList.size() == 0) {
            return starAmount;
        }
        //每个词条解锁一颗星
        //洗满再一颗星
        //洗满才解锁下一个词条
        starAmount += 2 * (paramList.size() - 1);

        starAmount++;
        String itemParam = paramSplit[paramList.size() - 1];
        String[] split = itemParam.split(";");
        int maxValue = Integer.parseInt(split[1]);
        FellingTreesEquipParam lastEquipParam = paramList.get(paramList.size() - 1);
        if (lastEquipParam.getValue() >= maxValue) {
            starAmount++;
        }

        return starAmount;
    }
}
